import os,django
#线下配置
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_demo.settings.offline_config")
#线上配置
# os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_demo.settings.online_config")
django.setup()

from elasticsearch import Elasticsearch
from ssl import create_default_context
#线下配置
context = create_default_context(cafile="C:/Users/123/Desktop/yuanshen/ca/node.pem")
#线上配置
#context = create_default_context(cafile="/home/Genshin_Impact/ca/node.pem")
context.check_hostname = False
obj = Elasticsearch("https://127.0.0.1:9203",ssl_context=context, http_auth=('elastic','banben'))

from train.models import Share_Content

obj.indices.delete(index='king',ignore=[400,404],request_timeout=30)
obj.indices.create(index='king', body={
  "settings":{
     "number_of_shards":6,
     "number_of_replicas":	1
  },
  "mappings":
    {
        "properties": {
            "results": {
                "properties":
                    {
                        "name": {"type": "text", "analyzer": "ik_smart"},
                        "cover": {"type": "keyword"},
                        "cumulative_clicks": {"type": "integer"},
                        "release_time": {"type": "date", "format": "yyyy-MM--dd HH:mm:ss||yyyy-MM-dd||epoch_millis"},
                        "sharer": {
                            "properties": {
                            "name": {"type": "text", "analyzer": "ik_smart"},
                            "title": {"type": "text", "analyzer": "ik_smart"},
                            "user": {
                                "properties": {
                                "first_name": {"type": "keyword"},
                                "last_name": {"type": "keyword"},
                                "icon": {"type": "keyword"}
                            }},
                            "presentation": {"type": "text", "analyzer": "ik_max_word"}
                        }},
                        "id": {"type": "integer"},
                        "detail_page":{
                            "properties": {
                            "synopsis": {"type": "text", "analyzer": "ik_max_word"},
                            "video_link": {"type": "keyword"},
                            "faq": {"type": "text", "analyzer": "ik_max_word"},
                            "document_link": {"type": "keyword"},
                            "vcs": {"type": "keyword"}
                        }}
                    }}
                }}
        }
, ignore=400)
def date_up():
    try:
        x = Share_Content.objects.filter(is_delelte=False, is_show=True).all()
        num=0
        for i in x:
            num+=1
            name=i.name
            cover=str(i.cover)
            cumulative_clicks=i.cumulative_clicks
            release_time=i.release_time
            id=i.id
            print(id,release_time,cumulative_clicks,cover,name)
            sharer_name=i.sharer.name
            sharer_title=i.sharer.title
            sharer_presentation=i.sharer.presentation
            print(sharer_name,sharer_title,sharer_presentation)
            user_icon=str(i.sharer.user.icon)
            user_first_name=i.sharer.user.first_name
            user_last_name=i.sharer.user.last_name
            print(user_first_name,user_last_name,user_icon)
            detail_page_synopsis=i.detail_page.synopsis
            detail_page_video_link=i.detail_page.video_link
            detail_page_faq=i.detail_page.faq
            detail_page_document_link=i.detail_page.document_link
            detail_page_vcs=i.detail_page.vcs
            data={"results":{
                "name": name,
                "cover": cover,
                "cumulative_clicks": cumulative_clicks,
                "release_time": release_time,
                "sharer": {
                    "name": sharer_name,
                    "title": sharer_title,
                    "user": {
                        "first_name":user_first_name,
                        "last_name": user_last_name,
                        "icon": user_icon
                    },
                    "presentation": sharer_presentation
                },
                "id": id,
                "detail_page": {
                    "synopsis":detail_page_synopsis,
                    "video_link": detail_page_video_link,
                    "faq": detail_page_faq,
                    "document_link": detail_page_document_link,
                    "vcs": detail_page_vcs
                }
            }}
            print(data)
            obj.create(index='king', doc_type='_doc', id=num, body=data,request_timeout=30)
    except:
        print('error')

if __name__ == '__main__':
    date_up()